#!/bin/bash
#set -e
# 备份并清理，将mysql 表数据保留21天

dateDir=$(date +%Y%m%d)"_all"
backUpFolder=/zpaasssd/zcm/mysql_171_grafana_dashboard_bkp
host=10.10.168.67
port=20821
username="zcm9"
password="xkvYWk7@uK"

main(){

mkdir ${backUpFolder}/${dateDir}
#rm  -rf ${backUpFolder}/$(date -d "-21 day" +%Y%m%d)"_all".tar.gz

if [[ -f ${backUpFolder}/$(date -d "-21 day" +%Y%m%d)"_all".tar.gz ]];then
  rm  -rf ${backUpFolder}/$(date -d "-21 day" +%Y%m%d)"_all".tar.gz
fi

mysqldump -h $host -P $port -u ${username} -p${password}  --databases zcm_grafana --default-character-set utf8  > ${backUpFolder}/${dateDir}/zcm_grafana.sql
mysqldump -h $host -P $port -u ${username} -p${password}  --databases zcm_grafpub --default-character-set utf8 > ${backUpFolder}/${dateDir}/zcm_grafpub.sql
#mysqldump -h $host -P $port -u ${username} -p${password}  --databases zcm_nms --default-character-set utf8 > ${backUpFolder}/${dateDir}/zcm_nms.sql
mysqldump -h $host -P $port -u ${username} -p${password}  --databases zcm_task --default-character-set utf8 > ${backUpFolder}/${dateDir}/zcm_task.sql
mysqldump -h $host -P $port -u ${username} -p${password}  --databases zcm_dialing --default-character-set utf8 > ${backUpFolder}/${dateDir}/zcm_dialing.sql
#mysqldump -h $host -P $port -u ${username} -p${password}  --databases zcm_idocs --default-character-set utf8 > ${backUpFolder}/${dateDir}/zcm_idocs.sql
mysqldump -h $host -P $port -u ${username} -p${password}  --databases nms_sla --default-character-set utf8 > ${backUpFolder}/${dateDir}/nms_sla.sql

#压缩备份文件,清理原始文件
cd ${backUpFolder}
tar zcvf ${dateDir}.tar.gz ${dateDir}
rm -rf ${backUpFolder}/${dateDir}

# 解压
#tar zxvf /zpaasssd/zcm/mysql_171_grafana_dashboard_bkp.tar.gz
echo success
}

main
